/**
 * Octopus, an XMMS2 client.
 * Copyright (C) 2014 Octopus Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace Octopus {
	public class Album : MedialibTracklist, IMedialibInfo, IComparable  {
		public Xmms.Value albuminfo { get; construct; }

		private Gee.ArrayList<int> mid_list;

		public Album (Xmms.Value albuminfo) {
			Object (albuminfo: albuminfo);
		}

		construct {
			string album;
			string artist;
			int    partofset;
			string performer;
			string picture_front;

			albuminfo.dict_entry_get_string ("album", out album);
			if (album != null) this.album = album;
			albuminfo.dict_entry_get_string ("artist", out artist);
			if (artist != null) this.artist = artist;
			albuminfo.dict_entry_get_int    ("partofset", out partofset);
			this.partofset = partofset;
			albuminfo.dict_entry_get_string ("performer", out performer);
			if (performer != null) this.performer = performer;
			albuminfo.dict_entry_get_string ("picture_front", out picture_front);
			if (picture_front != null) this.picture_front = picture_front;
			this.stored_in_medialib = true;
		}

		public void request_data () {
			add (this);
			add_finished ();
		}

		public string get_compare_string () {
			return get_artist_performer ();
		}

		public string get_artist_performer () {
			if ((performer != null) && (performer != "")) {
				return performer;
			}
			return artist;
		}

		public string get_album_partofset () {
			if (partofset == 0) {
				return album;
			} else {
				return album + " (" + "%d".printf (partofset) + ")";
			}
		}

		public void set_album_art (string filename, Gee.ArrayList<int> id_list) {
			mid_list = id_list;
			Gdk.Pixbuf pixbuf;
			try {
				pixbuf = new Gdk.Pixbuf.from_file (filename);
			} catch (GLib.Error error) {
				GLib.warning ("%s", error.message);
				Global.get ().message ("error: %s".printf (error.message));
				return;
			}
			uint8[] buffer;
			if (!pixbuf.save_to_buffer (out buffer, "png"))
				return;

			Global.get ().get_xmms_client ().get_xmms ().bindata_add (buffer).notifier_set ((/*Xmms.Value*/value) => {
				string hash;
				if (!value.get_string (out hash)) {
					string error;
					value.get_error (out error);
					GLib.warning ("%s", error);
					Global.get ().message ("error: %s".printf (error));
					return true;
				}
				foreach (int mid in mid_list) {
					Global.get ().get_xmms_client ().get_xmms ().medialib_entry_property_set_str (mid, "picture_front", hash);
					Global.get ().get_xmms_client ().get_xmms ().medialib_entry_property_set_str (mid, "picture_front_mime", "png");
				}
				return true;
			});
		}
	}
}
